<template>
<div>
    <el-card style="margin-bottom:20px; " >
        <el-breadcrumb separator-class="el-icon-arrow-right" style="margin:20px">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item to="">志愿者管理</el-breadcrumb-item>
          <el-breadcrumb-item to="">志愿者招聘</el-breadcrumb-item>
        </el-breadcrumb>
        <el-row :gutter="20">
          <el-col :span="8">
              <el-input placeholder="请输入内容">
                  <el-button slot="append" icon="el-icon-search"></el-button>
              </el-input>
          </el-col>
          <el-col :span="8">
              <el-button type="primary"  @click="addDialogVisible = true">添加志愿者招聘信息</el-button>
        </el-col>
        </el-row>    
    </el-card>
    <el-card style="margin-bottom:20px;" >
        <el-table :data="tableData" height="450px" stripe >
           <el-table-column label="招聘标题" prop="total"></el-table-column>
           <el-table-column label="职位" prop="position"></el-table-column>
           <el-table-column label="招聘要求" prop="workRequire" show-overflow-tooltip></el-table-column>
            <el-table-column label="工资待遇" prop="workSalary" show-overflow-tooltip></el-table-column>
            <el-table-column label="工作地点" prop="workPlace" show-overflow-tooltip></el-table-column>
            <el-table-column label="工作时间" prop="workTime" show-overflow-tooltip></el-table-column>
            <el-table-column label="招聘人数" prop="recruitsNum"></el-table-column>
            <el-table-column label="招聘时间" prop="recruitsTime"></el-table-column>
            <el-table-column label="截止时间" prop="recruitsEndTime"></el-table-column>
            <el-table-column label="负责人" prop="lead"></el-table-column>
            <el-table-column label="联系方式" prop="contact"></el-table-column>
           <el-table-column label="图片" prop="picture">
               <template slot-scope="scope">
                   <img width="100px" :src="scope.row.picture" alt="">
               </template>
           </el-table-column>
             <el-table-column label="创建时间" prop="createTime"></el-table-column>
           <el-table-column label="发布日期" prop="releaseDate"></el-table-column>
           <el-table-column label="最近点击时间" prop="lastClickTime"></el-table-column>
           <el-table-column label="点击次数" prop="clickNum"></el-table-column>
         <el-table-column label="操作" width="160" align="center">
               <template slot-scope="scope">
                   <el-button type="primary" size="mini" @click="updatebefore1(scope.$index,scope.row)">编辑</el-button>
                   <el-button type="danger" size="mini"  @click="del(scope.$index, scope.row.id)">删除</el-button>
               </template>
           </el-table-column>
        </el-table>
        <div style="margin:20px">
        
         <el-pagination
            layout="prev, pager, next"
            :total="total"
            :page-size="pageSize"
            background
            @current-change="current"
          >
        </el-pagination>
         <!-- 志愿者招聘添加 -->
         <el-dialog title="添加志愿者招聘信息" :visible.sync="addDialogVisible" width="40%">
            <el-form :model="addForm">
              <el-form-item label="招聘标题:">
                  <el-input type="text" v-model="addForm.total"></el-input>
              </el-form-item>
               <el-form-item label="职位:">
                  <el-input type="text" v-model="addForm.position"></el-input>
              </el-form-item>
               <el-form-item label="招聘要求:">
                  <el-input type="text" v-model="addForm.workRequire"></el-input>
              </el-form-item>
               <el-form-item label="工资待遇:">
                  <el-input type="text" v-model="addForm.workSalary"></el-input>
              </el-form-item>
               <el-form-item label="工作地点:">
                  <el-input type="text" v-model="addForm.workPlace"></el-input>
              </el-form-item>
                <el-form-item label="工作时间:">
                  <el-input type="text" v-model="addForm.workTime"></el-input>
              </el-form-item>
               <el-form-item label="招聘人数:">
                  <el-input type="text" v-model="addForm.recruitsNum"></el-input>
              </el-form-item>
               <el-form-item label="招聘时间:">
                  <el-input type="text" v-model="addForm.recruitsTime"></el-input>
              </el-form-item>
               <el-form-item label="截止时间:">
                  <el-input type="text" v-model="addForm.recruitsEndTime"></el-input>
              </el-form-item>
                <el-form-item label="负责人:">
                  <el-input type="text" v-model="addForm.lead"></el-input>
              </el-form-item>
               <el-form-item label="联系方式:">
                  <el-input type="text" v-model="addForm.contact"></el-input>
              </el-form-item>
              
               <el-form-item label="图片:">
              <el-upload 
              class="upload-demo" 
              action="action" 
              accept="image/jpeg,image/jpg,image/png"
              :limit="1"
              :auto-upload="true"
              :http-request="uploadFileAdd"
              ref="upload"
              multiple
              >
              <el-button type="primary" size="mini">点击上传</el-button>
              <div slot="tip" class="el_upload_tip">只能上传图片</div>
              </el-upload>
            </el-form-item>
               <el-form-item label="创建时间:">
                  <el-input type="text" v-model="addForm.createTime"></el-input>
              </el-form-item>
               <el-form-item label="发布日期:">
                  <!-- <el-input type="text" v-model="addForm.releaseDate"></el-input> -->
                  <el-date-picker type="datetime" placeholder="选择日期" value-format="yyyy-MM-dd HH:mm:ss" v-model="addForm.releaseDate"></el-date-picker>
              </el-form-item>
               <el-form-item label="最近点击时间:">
                  <el-input type="text" v-model="addForm.lastClickTime"></el-input>
              </el-form-item>
               <el-form-item label="点击次数:">
                  <el-input type="text" v-model="addForm.clickNum"></el-input>
              </el-form-item>
            
            </el-form>
            <span slot="footer">
                <el-button @click="addDialogVisible = false">取消</el-button>
                <el-button type="primary" @click="addVolunteer">确定</el-button>
            </span>
        </el-dialog>
         <!-- 志愿者招聘修改 -->
         <el-dialog title="编辑志愿者招聘信息" :visible.sync="updateDialogVisible" width="40%">
            <el-form :model="updateForm">
             <el-form-item label="招聘标题:">
                  <el-input type="text" v-model="updateForm.total"></el-input>
              </el-form-item>
               <el-form-item label="职位:">
                  <el-input type="text" v-model="updateForm.position"></el-input>
              </el-form-item>
               <el-form-item label="招聘要求:">
                  <el-input type="text" v-model="updateForm.workRequire"></el-input>
              </el-form-item>
               <el-form-item label="工资待遇:">
                  <el-input type="text" v-model="updateForm.workSalary"></el-input>
              </el-form-item>
               <el-form-item label="工作地点:">
                  <el-input type="text" v-model="updateForm.workPlace"></el-input>
              </el-form-item>
                <el-form-item label="工作时间:">
                  <el-input type="text" v-model="updateForm.workTime"></el-input>
              </el-form-item>
               <el-form-item label="招聘人数:">
                  <el-input type="text" v-model="updateForm.recruitsNum"></el-input>
              </el-form-item>
               <el-form-item label="招聘时间:">
                  <el-input type="text" v-model="updateForm.recruitsTime"></el-input>
              </el-form-item>
               <el-form-item label="截止时间:">
                  <el-input type="text" v-model="updateForm.recruitsEndTime"></el-input>
              </el-form-item>
                <el-form-item label="负责人:">
                  <el-input type="text" v-model="updateForm.lead"></el-input>
              </el-form-item>
               <el-form-item label="联系方式:">
                  <el-input type="text" v-model="updateForm.contact"></el-input>
              </el-form-item>
               <el-form-item label="图片:">
                <el-input type="hidden" v-model="updateForm.picture"></el-input>
               <el-upload 
              class="upload-demo" 
              action="action" 
              accept="image/jpeg,image/jpg,image/png"
              :limit="1"
              :auto-upload="true"
              :http-request="uploadFileEdit"
              ref="upload"
              multiple
              >
              <el-button type="primary" size="mini">点击上传</el-button>
              <div slot="tip" class="el_upload_tip">只能上传图片</div>
              </el-upload>
            </el-form-item>
               <el-form-item label="创建时间:">
                  <el-input type="text" v-model="updateForm.createTime" disabled></el-input>
              </el-form-item>
               <el-form-item label="发布日期:">
                 
                  <el-input type="text" v-model="updateForm.releaseDate"></el-input>
              </el-form-item>
               <el-form-item label="最近点击时间:">
                  <el-input type="text" v-model="updateForm.lastClickTime" disabled></el-input>
              </el-form-item>
               <el-form-item label="点击次数:">
                  <el-input type="text" v-model="updateForm.clickNum"></el-input>
              </el-form-item>
            </el-form>
            <span slot="footer">
                <el-button @click="updateDialogVisible = false">取消</el-button>
                <el-button type="primary" @click="updateVolunteer">确定</el-button>
            </span>
        </el-dialog>
        </div>
    </el-card>

</div>
</template>
<script>
export default {
  data() {
    return {
      total: 3,
      imgurl:'',
      updateimgurl:'',
      tableData: [],
      pageSize: 3,
      pageNum: 1,
      addForm: {},
      updateForm: {},
      addDialogVisible: false,
      updateDialogVisible: false,
    };
  },
  methods: {
    async getList(pageNum, pageSize) {
      // let {data:res} = await this.$axios.get('data/fupin.json');
      // // window.console.log(res.data.list);
      // this.tableData=res.data.list
      let { data: res } = await this.$axios.get("/volunteer/getlist", {
        params: {
          pageNum,
          pageSize,
        },
      });
      this.total = res.data.total;
      this.tableData = res.data.list;
    },
    current(e) {
    //   window.console.log(e);
      this.pageNum=e;
      this.getList(e, this.pageSize);
    },
    async addVolunteer() {
      this.addForm.picture=this.imgurl;
      window.console.log(JSON.stringify(this.addForm));
      //  window.console.log(this.addForm);
      //前段格式化
      //  let addform=JSON.stringify(this.addForm,null,2);
      let { data: res } = await this.$axios.post("/volunteer/add",this.addForm
);
      window.console.log(res.code); //目标值为200
      if (res.code != 200) return this.$message.error(res.message);
      this.$message.success(res.code);
      this.getList(this.pageNum,this.pageSize)
      this.addDialogVisible = false;
    },

      async uploadFileAdd(){
      
      const formData=new FormData();
      const file=this.$refs.upload.uploadFiles[0];
      const headerConfig={headers:{'Content-Type':'multipart/form-data'}}
      if(!file){
        alert('请选择文件');
        return;
      }
      window.console.log('图片解析为：'+file.raw);
      formData.append('file',file.raw);
      formData.append('type','volunteer');
      let {data:res}=await this.$axios.post('/uploadImg',formData,headerConfig)
      window.console.log(res);
      window.console.log(res.data.imgurl);
      this.imgurl=res.data.imgurl;
     },
    async uploadFileEdit(){
      const formData=new FormData();
      const file=this.$refs.upload.uploadFiles[0];
      const headerConfig={headers:{'Content-Type':'multipart/form-data'}}
      if(!file){
        alert('请选择文件');
        return;
      }
      window.console.log('图片解析为：'+file.raw);
      formData.append('file',file.raw);
      formData.append('type','user');
      let {data:res}=await this.$axios.post('/uploadImg',formData,headerConfig)
      window.console.log(res);
      window.console.log(res.data.imgurl);
      this.updateimgurl=res.data.imgurl;
      

},
      async updatebefore1(index, row) {
      // window.console.log(index);
      //  window.console.log(row);
       let { data: res } = await this.$axios.get("/volunteer/getlist", {
        params: {
          pageNum:this.pageNum,
          pageSize:this.pageSize,
          id:row.id
        },
      });
      this.updateForm = res.data.list[0];
      this.updateDialogVisible = true;
    },
    async updateVolunteer() {
       if(this.updateimgurl !=''){
        this.updateForm.picture = this.updateimgurl;
        this.updateimgurl = '';
        // window.console.log(this.updateForm.picture);
      }
    //   window.console.log(JSON.stringify(this.updateForm));
      let { data: res } = await this.$axios.post("/volunteer/update", this.updateForm);
      if (res.code != 200) return this.$message.error(res.code);
      this.$message.success(res.code);
       window.console.log(res)
      this.updateDialogVisible = false;
      this.getList(this.pageNum,this.pageSize);
    },
    async del(index, id) {
    //   window.console.log(id);
       let { data: res } = await this.$axios.get("/volunteer/delete", {
        params: {
          id: id,
        },
      });
      if (res.code != 200) return this.$message.error(res.code);
      this.$message.success(res.code);
      this.getList(this.pageNum,this.pageSize);
    },
  },
  created() {
    this.getList(this.pageNum, this.pageSize);
  },
};
</script>

<style scoped>

</style>